home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / WINSRC20.ZIP / MISCRES.C < prev    next >
C/C++ Source or Header  |  1990-10-06  |  14KB  |  486 lines

  1. /*
  2.     Resident odds and ends that don't fit anywhere else.
  3. */
  4.  
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include <time.h>
  10. #include <stdarg.h>
  11. #ifdef __TURBOC__
  12. #include <dir.h>
  13. #endif
  14. #include "fractint.h"
  15. #include "fractype.h"
  16.  
  17. /* routines in this module    */
  18.  
  19. extern    void restore_active_ovly(void );
  20. extern    void findpath(char *filename,char *fullpathname);
  21. extern    void notdiskmsg(void );
  22. extern    int cvtcentermag(double *Xctr,double *Yctr,double *Magnification);
  23. extern    void updatesavename(char *name);
  24. extern    int check_key(void );
  25. extern    int timer(int timertype,int(*subrtn)(),...);
  26. extern    void showtrig(char *buf);
  27. extern    int set_trig_array(int k,char *name);
  28. extern    void set_trig_pointers(int which);
  29. extern    int tab_display(void );
  30. extern    int endswithslash(char *fl);
  31.  
  32.  
  33. int active_ovly = -1;
  34. long timer_start,timer_interval;    /* timer(...) start & total */
  35.  
  36. extern int  active_ovly;
  37. extern int  xdots, ydots;
  38. extern int  dotmode;
  39. extern int  show_orbit;
  40. extern int  timerflag;
  41. extern int  debugflag;
  42. extern int  maxit;
  43. extern int  fractype;
  44.  
  45.  
  46. /* call next when returning from resident routine and unsure whether
  47.    caller is an overlay which has been displaced */
  48. void restore_active_ovly()
  49. {
  50.    switch (active_ovly) {
  51.       case OVLY_MISCOVL:  miscovl_overlay();  break;
  52.       case OVLY_CMDFILES: cmdfiles_overlay(); break;
  53.       case OVLY_HELP:      help_overlay();     break;
  54.       case OVLY_PROMPTS:  prompts_overlay();  break;
  55.       case OVLY_LOADFILE: loadfile_overlay(); break;
  56.       case OVLY_ROTATE:   rotate_overlay();   break;
  57.       case OVLY_PRINTER:  printer_overlay();  break;
  58.       case OVLY_LINE3D:   line3d_overlay();   break;
  59.       case OVLY_ENCODER:  encoder_overlay();  break;
  60.       case OVLY_CALCFRAC: calcfrac_overlay(); break;
  61.       }
  62. }
  63.  
  64.  
  65. void findpath(char *filename, char *fullpathname) /* return full pathnames */
  66. {
  67.    fullpathname[0] = 0;             /* indicate none found */
  68. #ifdef __TURBOC__                /* look for the file */
  69.    strcpy(fullpathname,searchpath(filename));
  70. #else
  71.    _searchenv(filename,"PATH",fullpathname);
  72. #endif
  73.    if (fullpathname[0] != 0)            /* found it! */
  74.       if (strncmp(&fullpathname[2],"\\\\",2) == 0) /* stupid klooge! */
  75.      strcpy(&fullpathname[3],filename);
  76. }
  77.  
  78.  
  79. void notdiskmsg()
  80. {
  81. static char far sorrymsg[]={"\
  82. I'm sorry, but because of its random-screen-access algorithms, this\n\
  83. type cannot be created using a real-disk based 'video' mode."};
  84.    stopmsg(1,sorrymsg);
  85. }
  86.  
  87.  
  88. /* convert corners to center/mag */
  89. int cvtcentermag(double *Xctr, double *Yctr, double *Magnification)
  90. {
  91.    extern double xxmax,xxmin,yymax,yymin,xx3rd,yy3rd;
  92.    double Width, Height, Radius, Ratio;
  93.    Width  = xxmax - xxmin;
  94.    Height = yymax - yymin;
  95.    if(xx3rd != xxmin || yy3rd != yymin || Width < 0
  96.      || (Ratio = Height / Width) < 0.749 || Ratio > 0.751)
  97.       return(0);
  98.    /* calculate center and magnification */
  99.    Radius = Height / 2.0;
  100.    *Xctr = xxmin + (Width / 2.0);
  101.    *Yctr = yymin + Radius;
  102.    *Magnification = 1.0 / Radius;
  103.    return(1);
  104. }
  105.  
  106.  
  107. void updatesavename(char *filename) /* go to the next file name */
  108. {
  109.    char *save, *hold;
  110.    char name[80],suffix[80];
  111.    char *dotptr;
  112.  
  113.    strcpy(name,filename);
  114.    suffix[0] = 0;
  115.    if ((dotptr = strrchr(name,'.')) != NULL
  116.      && dotptr > strrchr(name,'\\')) {
  117.       strcpy(suffix,dotptr);
  118.       *dotptr = 0;
  119.       }
  120.  
  121.    hold = name + strlen(name) - 1; /* start at the end */
  122.    while(hold >= name && (*hold == ' ' || isdigit(*hold))) /* skip backwards */
  123.       hold--;
  124.    hold++;            /* recover first digit */
  125.    while (*hold == '0')         /* skip leading zeros */
  126.       hold++;
  127.    save = hold;
  128.    while (*save) {        /* check for all nines */
  129.       if (*save != '9')
  130.      break;
  131.       save++;
  132.       }
  133.    if (!*save)            /* if the whole thing is nines then back */
  134.       save = hold - 1;        /* up one place. Note that this will eat */
  135.                 /* your last letter if you go to far.     */
  136.    else
  137.       save = hold;
  138.    itoa(atoi(hold) + 1, save, 10); /* increment the number */
  139.    strcpy(filename,name);
  140.    strcat(filename,suffix);
  141. }
  142.  
  143.  
  144. int check_key()
  145. {
  146.    int key;
  147.    if((key = keypressed()) != 0) {
  148.       if(key != 'o' && key != 'O')
  149.      return(-1);
  150.       getakey();
  151.       if (dotmode != 11)
  152.      show_orbit = 1 - show_orbit;
  153.    }
  154.    return(0);
  155. }
  156.  
  157.  
  158. /* timer function:
  159.      timer(0,(*fractal)())        fractal engine
  160.      timer(1,NULL,int width)        decoder
  161.      timer(2,NULL,char *savename)    encoder
  162.   */
  163. int timer(int timertype,int(*subrtn)(),...)
  164. {
  165.    va_list arg_marker;    /* variable arg list */
  166.    char *savename;
  167.    char *timestring;
  168.    time_t ltime;
  169.    FILE *fp;
  170.    int out;
  171.    int i;
  172.    int do_bench;
  173.  
  174.    va_start(arg_marker,subrtn);
  175.    do_bench = timerflag; /* record time? */
  176.    if (timertype == 2)     /* encoder, record time only if debug=200 */
  177.       do_bench = (debugflag == 200);
  178.    if(do_bench)
  179.       fp=fopen("bench","a");
  180.    timer_start = clock();
  181.    switch(timertype) {
  182.       case 0:
  183.      out = (*subrtn)();
  184.      break;
  185.       case 1:
  186.      i = va_arg(arg_marker,int);
  187.      out = decoder(i);         /* not indirect, safer with overlays */
  188.      break;
  189.       case 2:
  190.      savename = va_arg(arg_marker,char *);
  191.      out = encoder();         /* not indirect, safer with overlays */
  192.      break;
  193.       }
  194.    /* next assumes CLK_TCK is 10^n, n>=2 */
  195.    timer_interval = (clock() - timer_start) / (CLK_TCK/100);
  196.  
  197.    if(do_bench) {
  198.       time(<ime);
  199.       timestring = ctime(<ime);
  200.       timestring[24] = 0; /*clobber newline in time string */
  201.       switch(timertype) {
  202.      case 1:
  203.         fprintf(fp,"decode ");
  204.         break;
  205.      case 2:
  206.         fprintf(fp,"encode ");
  207.         break;
  208.      }
  209.       fprintf(fp,"%s type=%s resolution = %dx%d maxiter=%d",
  210.       timestring,
  211.       fractalspecific[fractype].name,
  212.       xdots,
  213.       ydots,
  214.       maxit);
  215.       fprintf(fp," time= %ld.%02ld secs\n",timer_interval/100,timer_interval%100);
  216.       if(fp != NULL)
  217.      fclose(fp);
  218.       }
  219.    return(out);
  220. }
  221.  
  222.  
  223. extern void lStkSin(void), dStkSin(void), lStkCos(void), dStkCos(void);
  224. extern void lStkSinh(void),dStkSinh(void),lStkCosh(void),dStkCosh(void);
  225. extern void lStkExp(void), dStkExp(void), lStkLog(void), dStkLog(void);
  226. extern void lStkSqr(void), dStkSqr(void);
  227.  
  228. unsigned char trigndx[] = {SIN,SQR,SINH,COSH};
  229. void (*ltrig0)() = lStkSin;
  230. void (*ltrig1)() = lStkSqr;
  231. void (*ltrig2)() = lStkSinh;
  232. void (*ltrig3)() = lStkCosh;
  233. void (*dtrig0)() = dStkSin;
  234. void (*dtrig1)() = dStkSqr;
  235. void (*dtrig2)() = dStkSinh;
  236. void (*dtrig3)() = dStkCosh;
  237.  
  238. struct trig_funct_lst trigfn[] =
  239. /* changing the order of these alters meaning of *.fra file */
  240. {
  241.    {"sin", lStkSin, dStkSin },
  242.    {"cos", lStkCos, dStkCos },
  243.    {"sinh",lStkSinh,dStkSinh},
  244.    {"cosh",lStkCosh,dStkCosh},
  245.    {"exp", lStkExp, dStkExp },
  246.    {"log", lStkLog, dStkLog },
  247.    {"sqr", lStkSqr, dStkSqr }
  248. };
  249.  
  250. void showtrig(char *buf) /* return display form of active trig functions */
  251. {
  252.    int numfn,i;
  253.    char tmpbuf[40];
  254.    numfn = (fractalspecific[fractype].flags >> 6) & 7;
  255.    *buf = 0; /* null string if none */
  256.    if (numfn) {
  257.       sprintf(buf, " function=%s",trigfn[trigndx[0]].name);
  258.       i = 0;
  259.       while(++i < numfn) {
  260.      sprintf(tmpbuf, "/%s",trigfn[trigndx[i]].name);
  261.      strcat(buf,tmpbuf);
  262.      }
  263.       }
  264. }
  265.  
  266. /* set array of trig function indices according to "function=" command */
  267. int set_trig_array(int k, char *name)
  268. {
  269.    char trigname[6];
  270.    int i, lstlen;
  271.    char *slash;
  272.    strncpy(trigname,name,5);
  273.    trigname[5] = 0; /* safety first */
  274.  
  275.    if ((slash = strchr(trigname,'/')))
  276.       *slash = 0;
  277.  
  278.    strlwr(trigname);
  279.    lstlen = sizeof(trigfn)/sizeof(struct trig_funct_lst);
  280.  
  281.    for(i=0;i<lstlen;i++)
  282.    {
  283.       if(strcmp(trigname,trigfn[i].name)==0)
  284.       {
  285.      trigndx[k] = i;
  286.      set_trig_pointers(k);
  287.      break;
  288.       }
  289.    }
  290.    return(0);
  291. }
  292. void set_trig_pointers(int which)
  293. {
  294.   /* set trig variable functions to avoid array lookup time */
  295.    int i;
  296.    switch(which)
  297.    {
  298.    case 0:
  299.       ltrig0 = trigfn[trigndx[0]].lfunct;
  300.       dtrig0 = trigfn[trigndx[0]].dfunct;
  301.       break;
  302.    case 1:
  303.